package com.mid;

import java.util.Iterator;

/**
 * Created by Lxk on 2020/2/29.
 */
class PeekingIterator implements Iterator<Integer> {

    private Iterator<Integer> iterator;

    private Integer valTop = null;

    public PeekingIterator(Iterator<Integer> iterator) {
        // initialize any member here.
        this.iterator = iterator;
    }

    // Returns the next element in the iteration without advancing the iterator.
    public Integer peek() {
        if (valTop == null && iterator.hasNext()) {
            valTop = iterator.next();
        }
        return valTop;
    }

    // hasNext() and next() should behave the same as in the Iterator interface.
    // Override them if needed.
    @Override
    public Integer next() {
        Integer res = null;
        if (valTop != null) {
            res = valTop;
            valTop = null;
            return res;
        }
        return iterator.next();
    }

    @Override
    public boolean hasNext() {
        return iterator.hasNext() || valTop != null;
    }
}

public class Solution284 {

    public static void main(String[] args) {

    }
}
